/*
 * Sesame Inc.
 * Copyright (c) 2009 All Rights Reserved.
 */
package com.sesame.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.sesame.domain.Album;
import com.sesame.domain.Collect;
import com.sesame.domain.Friend;
import com.sesame.domain.Guestbook;
import com.sesame.domain.Image;
import com.sesame.domain.User;
import com.sesame.service.AlbumService;
import com.sesame.service.ContactService;
import com.sesame.service.PageService;
import com.sesame.service.UserService;
import com.sesame.toolkit.Encrypt;
import com.sesame.toolkit.Page;

/**
 * @author voidman
 * 
 */
public class UserHomeController extends AbstractController {

    private AlbumService albumService;
    private UserService userService;
    private ContactService contactService;
    private PageService pageService;
    private String content = null;

    public void setPageService(PageService pageService) {
        this.pageService = pageService;
    }

    public void setAlbumService(AlbumService albumService) {
        this.albumService = albumService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public void setContactService(ContactService contactService) {
        this.contactService = contactService;
    }

    @SuppressWarnings("unchecked")
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        Integer user1Id = (Integer) request.getSession().getAttribute("userId");
        Integer user2Id = Integer.parseInt(request.getParameter("userId"));
        Map model = new HashMap();
        if (user1Id == null) {
            List<Album> albumList = null;
            List<Image> imageList = null;

            User user = userService.showUser(user2Id);
            albumList = albumService.listTopAlbumByUserId(user2Id);
            imageList = albumService.listTopImage(user2Id);

            Album album = null;
            for (int i = 0; i < albumList.size(); i++) {
                album = albumList.get(i);
                if (album.getPrivacy().equals("2")
                        || album.getPrivacy().equals("1")) {
                    album.setCover(Encrypt.MapEncrypt("00000/001"));

                }
            }

            Image image = null;
            for (int i = 0; i < imageList.size(); i++) {
                image = imageList.get(i);
                if (image.getPrivacy().equals("2")
                        || album.getPrivacy().equals("1")) {
                    image.setUrl(Encrypt.MapEncrypt("00000/001"));

                }
            }

            List<Guestbook> guestbookList = contactService
                    .listTopGuestbook(user2Id);

            model.put("user", user);
            model.put("writerId", 0);
            model.put("albumList", albumList);
            model.put("imageList", imageList);
            model.put("guestbookList", guestbookList);

            return new ModelAndView("userHome", model);

        } else {
            if (user1Id.intValue() == user2Id.intValue()) {
                return new ModelAndView("redirect:home.html");
            }
            String nickName = (String) request.getSession().getAttribute(
                    "nickName");
            String headPic = (String) request.getSession().getAttribute(
                    "headPic");
            content = request.getParameter("content");

            boolean isFriend = contactService.isFriend(user2Id, user1Id);
            boolean isMyFriend = contactService.isFriend(user1Id, user2Id);
            List<Album> albumList = null;
            List<Image> imageList = null;
            if (content != null) {
                contactService.newGuestbook(user1Id, user2Id, content,
                        nickName, headPic);
            }

            User user = userService.showUser(user2Id);
            albumList = albumService.listTopAlbumByUserId(user2Id);
            imageList = albumService.listTopImage(user2Id);

            Album album = null;
            for (int i = 0; i < albumList.size(); i++) {
                album = albumList.get(i);
                if (album.getPrivacy().equals("2")) {
                    album.setCover(Encrypt.MapEncrypt("00000/001"));

                }
            }
            if (!isFriend) {
                for (int i = 0; i < albumList.size(); i++) {
                    album = albumList.get(i);
                    if (album.getPrivacy().equals("1")) {
                        album.setCover(Encrypt.MapEncrypt("00000/001"));

                    }
                }
            }

            Image image = null;
            for (int i = 0; i < imageList.size(); i++) {
                image = imageList.get(i);
                if (image.getPrivacy().equals("2")) {
                    image.setUrl(Encrypt.MapEncrypt("00000/001"));

                }
            }
            if (!isFriend) {
                for (int i = 0; i < imageList.size(); i++) {
                    image = imageList.get(i);
                    if (image.getPrivacy().equals("1")) {
                        image.setUrl(Encrypt.MapEncrypt("00000/001"));

                    }
                }
            }

            List<Guestbook> guestbookList = contactService
                    .listTopGuestbook(user2Id);
            List<Friend> friendList = contactService.listTopFriend(user2Id);
            List<Collect> collectList = userService.listTopCollect(user2Id);

            model.put("user", user);
            model.put("writerId", user1Id);
            model.put("albumList", albumList);
            model.put("imageList", imageList);
            model.put("guestbookList", guestbookList);
            model.put("friendList", friendList);
            model.put("collectList", collectList);
            model.put("isMyFriend", isMyFriend);
            return new ModelAndView("userHome", model);

        }

    }

}